package com.cloudptt.wp.ui.address.map.adapter;

import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.MapPointInfo;
import com.mcptt.db.model.UserInfo;

public class MapUserListAdapter extends BaseAdapter {

	private static final String THIS_FILE = "MapUserListAdapter";
	private Context ctx;
	private List<UserInfo> list = null;
	private List<MapPointInfo> pointList = null;
	private List<String> senderList = null;

	public static final int SOURCE_MESSAGE = 1;
	public static final int SOURCE_SOS = 2;

	public MapUserListAdapter(Context ctx, List<UserInfo> list, List<MapPointInfo> pointList)
	{
		this.ctx= ctx;
		this.list = list;
		this.pointList = pointList;
		Log.i(THIS_FILE," MapUserListAdapter list = " + list);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}


	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;
		if (convertView == null)
		{
			LayoutInflater inflater = LayoutInflater.from(ctx);
			convertView = inflater.inflate(R.layout.person_map_item, null);

			viewHolder = new ViewHolder();
			viewHolder.name    =  (TextView)  convertView.findViewById(R.id.person_name);
			viewHolder.personIcon = (ImageView)convertView.findViewById(R.id.friend_pic);
			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		//		viewHolder.personIcon.setImageResource(R.drawable.groups_default_pic);

		final AddrInfo userInfo = list.get(position);
		Log.i(THIS_FILE," MapUserListAdapter userInfo = " + userInfo);
		String name = userInfo.getName();
		viewHolder.name.setText(name);
		
		String num = userInfo.getAddrId();
		boolean isReport = this.isReported(num);
		Log.i(THIS_FILE," MapUserListAdapter isReport = " + isReport);
		if(isReport)
		{
			viewHolder.personIcon.setImageResource(R.drawable.local_localed);
		}
		else
		{
			viewHolder.personIcon.setImageResource(R.drawable.local_unlocal);
		}

		return convertView;
	}

	private boolean isReported(String num)
	{
		if(num == null
				|| num.length() <= 0)
		{
			return false;
		}
		if(pointList == null)
		{
			return false;
		}
		for(Iterator<MapPointInfo> itor = pointList.iterator();itor.hasNext();)
		{
			MapPointInfo info = itor.next();
			UserInfo sender = info.getSender();
			if(sender != null)
			{
				String mcpttId = sender.getAddrId();
				Log.d(THIS_FILE," findUserNameByNum mcpttId = " + mcpttId + " num = " + num);
				if(mcpttId != null
						&& mcpttId.contains(num))
				{
					return true;
				}
			}
		}
		return false;
	}




	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		//		userId = config.getUserInfo().getNum();
		super.notifyDataSetChanged();
	}

	public final class ViewHolder {

		public TextView  name;
		public ImageView personIcon;
	}


}
